package com.erpanderp.wam.tm.model.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.tm.model.entity.RecruitmentRequest;
import com.erpanderp.wam.tm.model.entity.Skill;
import com.erpanderp.wam.util.HibernateUtil;

public class SkillDAO {

	public List<Skill> getSkills(){ 
		List<Skill> skills = new ArrayList<Skill>();
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Skill s";			
			Query query = session.createQuery(queryString);
			query.setCacheable(false);			
			skills =(List<Skill>) query.list();
			session.getTransaction().commit();
			return skills;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public Skill createSkill(Skill skill){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.setFlushMode(FlushMode.COMMIT);
		session.saveOrUpdate(skill);
		session.getTransaction().commit();
		return skill;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public Skill updateSkill(Skill skill){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.setFlushMode(FlushMode.COMMIT);
		session.saveOrUpdate(skill);
		session.getTransaction().commit();
		return skill;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
}
